using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
namespace INTEX.DataBase.CE
{
    public partial class Mapper
    {
    /*
      var qVKPOS = from m in mtemp.VKPOS select m;
       DBMapper.MapVKPOS(qVKPOS.ToList(), true);
       */
        public List<VKPOS> MapVKPOS(XElement item2, bool mergedb)
        {
            List<VKPOS> result = new List<VKPOS>();
            var item4 = from item3 in item2.Elements()
                        select new VKPOS
                        {  
                            MAINKEY = (string)item3.Element("MAINKEY"),
                            VKPMANDANT = (string)item3.Element("VKPMANDANT"),
                            VKPAUFKRS = (string)item3.Element("VKPAUFKRS"),
                            VKPAUFLFD = (string)item3.Element("VKPAUFLFD"),
                            VKPAUFPOS = (string)item3.Element("VKPAUFPOS"),
                            VKPNEUDAT = (string)item3.Element("VKPNEUDAT"),
                            VKPKUNDNR = (string)item3.Element("VKPKUNDNR"),
                            VKPARTST = (string)item3.Element("VKPARTST"),
                            VKPKOLL = (string)item3.Element("VKPKOLL"),
                            VKPART = (string)item3.Element("VKPART"),
                            VKPFARBE = (string)item3.Element("VKPFARBE"),
                            VKPMENGE1 = (string)item3.Element("VKPMENGE1"),
                            VKPMENGE2 = (string)item3.Element("VKPMENGE2"),
                            VKPRESERV = (string)item3.Element("VKPRESERV"),
                            VKPZUGET = (string)item3.Element("VKPZUGET"),
                            VKPFREIGE = (string)item3.Element("VKPFREIGE"),
                            VKPGAUFBEST = (string)item3.Element("VKPGAUFBEST"),
                            VKPKOMMISS = (string)item3.Element("VKPKOMMISS"),
                            VKPFAKT = (string)item3.Element("VKPFAKT"),
                            VKPVERS = (string)item3.Element("VKPVERS"),
                            VKPEINGET = (string)item3.Element("VKPEINGET"),
                            VKPOFFEN = (string)item3.Element("VKPOFFEN"),
                            VKPMEEH = (string)item3.Element("VKPMEEH"),
                            VKPLIEFTERM = (string)item3.Element("VKPLIEFTERM"),
                            PHOTO = (string)item3.Element("PHOTO")
                        }; 
                        
          result = item4.ToList();
          MapVKPOS(result, mergedb);
          return result;
        }
        ///DBMapper.MapVKPOS(mtemp.VKPOS.ToList<VKPOS>(), true);        
       public void MapVKPOS(List<VKPOS> result, bool mergedb)
        {
          if (mergedb)
            {     
            
                int loaded = 0;
                int page = 500;
                int count = result.Count();
                while (true)
                {
                    if (count - loaded < page) 
                    {
                        page = count - loaded;
                    }
                    var query =
                        (from VKPOS row in result.AsQueryable()
                         select row).Skip(loaded).Take(page);
               var rowsexist = from u in LocalDB.VKPOS.ToList().AsQueryable()
                                           join uc in query.AsQueryable() on  u.MAINKEY equals  uc.MAINKEY
                                           select u;
                LocalDB.VKPOS.DeleteAllOnSubmit(rowsexist);
                LocalDB.SubmitChanges();
                LocalDB.VKPOS.InsertAllOnSubmit(query);
                LocalDB.SubmitChanges();
                 loaded += page;
                if (count == loaded)
                        break;
                }
            }
       }
    }
    [Table(Name = "VKPOS")]
    public class VKPOS : TBBASE
    {
        private string _MAINKEY = "0";
        [Column(IsPrimaryKey = true, CanBeNull = false)]
        public string MAINKEY
        {
            get
            {
                return _MAINKEY;
            }
            set
            {
                if (value != _MAINKEY)
                {
                    _MAINKEY = value;
                    NotifyPropertyChanging("MAINKEY");                 
                }
            }
        }
        private string _Photo = "YES";
        [Column]
        public string PHOTO
        {
            get
            {
                return _Photo;
            }
            set
            {
                if (value != _Photo)
                {
                    _Photo = value;
                    NotifyPropertyChanging("PHOTO");
                }
            }
        } 
        [Column(IsVersion = true)]
        private Binary _version;
   
        private string  _VKPMANDANT="0";
        [Column]
        public string VKPMANDANT
        {
            get
            {
                return _VKPMANDANT;
            }
            set
            {
                if (value != _VKPMANDANT)
                {
                     _VKPMANDANT = value;
                    NotifyPropertyChanged("VKPMANDANT");
                }
            }
        }
        private string  _VKPAUFKRS="0";
        [Column]
        public string VKPAUFKRS
        {
            get
            {
                return _VKPAUFKRS;
            }
            set
            {
                if (value != _VKPAUFKRS)
                {
                     _VKPAUFKRS = value;
                    NotifyPropertyChanged("VKPAUFKRS");
                }
            }
        }
        private string  _VKPAUFLFD="0";
        [Column]
        public string VKPAUFLFD
        {
            get
            {
                return _VKPAUFLFD;
            }
            set
            {
                if (value != _VKPAUFLFD)
                {
                     _VKPAUFLFD = value;
                    NotifyPropertyChanged("VKPAUFLFD");
                }
            }
        }
        private string  _VKPAUFPOS="0";
        [Column]
        public string VKPAUFPOS
        {
            get
            {
                return _VKPAUFPOS;
            }
            set
            {
                if (value != _VKPAUFPOS)
                {
                     _VKPAUFPOS = value;
                    NotifyPropertyChanged("VKPAUFPOS");
                }
            }
        }
        private string  _VKPNEUDAT="0";
        [Column]
        public string VKPNEUDAT
        {
            get
            {
                return _VKPNEUDAT;
            }
            set
            {
                if (value != _VKPNEUDAT)
                {
                     _VKPNEUDAT = value;
                    NotifyPropertyChanged("VKPNEUDAT");
                }
            }
        }
        private string  _VKPKUNDNR="0";
        [Column]
        public string VKPKUNDNR
        {
            get
            {
                return _VKPKUNDNR;
            }
            set
            {
                if (value != _VKPKUNDNR)
                {
                     _VKPKUNDNR = value;
                    NotifyPropertyChanged("VKPKUNDNR");
                }
            }
        }
        private string  _VKPARTST="0";
        [Column]
        public string VKPARTST
        {
            get
            {
                return _VKPARTST;
            }
            set
            {
                if (value != _VKPARTST)
                {
                     _VKPARTST = value;
                    NotifyPropertyChanged("VKPARTST");
                }
            }
        }
        private string  _VKPKOLL="0";
        [Column]
        public string VKPKOLL
        {
            get
            {
                return _VKPKOLL;
            }
            set
            {
                if (value != _VKPKOLL)
                {
                     _VKPKOLL = value;
                    NotifyPropertyChanged("VKPKOLL");
                }
            }
        }
        private string  _VKPART="0";
        [Column]
        public string VKPART
        {
            get
            {
                return _VKPART;
            }
            set
            {
                if (value != _VKPART)
                {
                     _VKPART = value;
                    NotifyPropertyChanged("VKPART");
                }
            }
        }
        private string  _VKPFARBE="0";
        [Column]
        public string VKPFARBE
        {
            get
            {
                return _VKPFARBE;
            }
            set
            {
                if (value != _VKPFARBE)
                {
                     _VKPFARBE = value;
                    NotifyPropertyChanged("VKPFARBE");
                }
            }
        }
        private string  _VKPMENGE1="0";
        [Column]
        public string VKPMENGE1
        {
            get
            {
                return _VKPMENGE1;
            }
            set
            {
                if (value != _VKPMENGE1)
                {
                     _VKPMENGE1 = value;
                    NotifyPropertyChanged("VKPMENGE1");
                }
            }
        }
        private string  _VKPMENGE2="0";
        [Column]
        public string VKPMENGE2
        {
            get
            {
                return _VKPMENGE2;
            }
            set
            {
                if (value != _VKPMENGE2)
                {
                     _VKPMENGE2 = value;
                    NotifyPropertyChanged("VKPMENGE2");
                }
            }
        }
        private string  _VKPRESERV="0";
        [Column]
        public string VKPRESERV
        {
            get
            {
                return _VKPRESERV;
            }
            set
            {
                if (value != _VKPRESERV)
                {
                     _VKPRESERV = value;
                    NotifyPropertyChanged("VKPRESERV");
                }
            }
        }
        private string  _VKPZUGET="0";
        [Column]
        public string VKPZUGET
        {
            get
            {
                return _VKPZUGET;
            }
            set
            {
                if (value != _VKPZUGET)
                {
                     _VKPZUGET = value;
                    NotifyPropertyChanged("VKPZUGET");
                }
            }
        }
        private string  _VKPFREIGE="0";
        [Column]
        public string VKPFREIGE
        {
            get
            {
                return _VKPFREIGE;
            }
            set
            {
                if (value != _VKPFREIGE)
                {
                     _VKPFREIGE = value;
                    NotifyPropertyChanged("VKPFREIGE");
                }
            }
        }
        private string  _VKPGAUFBEST="0";
        [Column]
        public string VKPGAUFBEST
        {
            get
            {
                return _VKPGAUFBEST;
            }
            set
            {
                if (value != _VKPGAUFBEST)
                {
                     _VKPGAUFBEST = value;
                    NotifyPropertyChanged("VKPGAUFBEST");
                }
            }
        }
        private string  _VKPKOMMISS="0";
        [Column]
        public string VKPKOMMISS
        {
            get
            {
                return _VKPKOMMISS;
            }
            set
            {
                if (value != _VKPKOMMISS)
                {
                     _VKPKOMMISS = value;
                    NotifyPropertyChanged("VKPKOMMISS");
                }
            }
        }
        private string  _VKPFAKT="0";
        [Column]
        public string VKPFAKT
        {
            get
            {
                return _VKPFAKT;
            }
            set
            {
                if (value != _VKPFAKT)
                {
                     _VKPFAKT = value;
                    NotifyPropertyChanged("VKPFAKT");
                }
            }
        }
        private string  _VKPVERS="0";
        [Column]
        public string VKPVERS
        {
            get
            {
                return _VKPVERS;
            }
            set
            {
                if (value != _VKPVERS)
                {
                     _VKPVERS = value;
                    NotifyPropertyChanged("VKPVERS");
                }
            }
        }
        private string  _VKPEINGET="0";
        [Column]
        public string VKPEINGET
        {
            get
            {
                return _VKPEINGET;
            }
            set
            {
                if (value != _VKPEINGET)
                {
                     _VKPEINGET = value;
                    NotifyPropertyChanged("VKPEINGET");
                }
            }
        }
        private string  _VKPOFFEN="0";
        [Column]
        public string VKPOFFEN
        {
            get
            {
                return _VKPOFFEN;
            }
            set
            {
                if (value != _VKPOFFEN)
                {
                     _VKPOFFEN = value;
                    NotifyPropertyChanged("VKPOFFEN");
                }
            }
        }
        private string  _VKPMEEH="0";
        [Column]
        public string VKPMEEH
        {
            get
            {
                return _VKPMEEH;
            }
            set
            {
                if (value != _VKPMEEH)
                {
                     _VKPMEEH = value;
                    NotifyPropertyChanged("VKPMEEH");
                }
            }
        }
        private string  _VKPLIEFTERM="0";
        [Column]
        public string VKPLIEFTERM
        {
            get
            {
                return _VKPLIEFTERM;
            }
            set
            {
                if (value != _VKPLIEFTERM)
                {
                     _VKPLIEFTERM = value;
                    NotifyPropertyChanged("VKPLIEFTERM");
                }
            }
        }
      public void ConvertToFile(IsolatedStorageFile ISO,ObservableCollection<VKPOS> ListInput, string filep = "", string setguid="")
        {           
            try
            {
                string localfile = filep;
                if (filep.Trim() == "")
                {
                    if (setguid.Trim() == "")
                        setguid = Guid.NewGuid().ToString();
                    localfile = "Data\\PhoneDB\\VKPOS\\VKPOS" +setguid + ".xml";
                }
                string folder = "Data\\PhoneDB\\VKPOS" ;
                if (!ISO.DirectoryExists(folder))
                {
                    ISO.CreateDirectory(folder);
                }
                ISO.DeleteFile(localfile);
                StreamWriter sw = new StreamWriter(ISO.CreateFile(localfile));
                XDocument xdoc = new XDocument();
                //Root Element
                XElement root = new XElement("VKPOS");
                if (setguid.Trim() != "")
                {
                    root = new XElement("ITX"+setguid);
                }
                //Add root to file
                xdoc.Add(root);
                foreach (VKPOS row in ListInput)
                {
                    XElement f = new XElement("VKPOS");
                    XElement MAINKEY = new XElement("MAINKEY");
                     f.Add(MAINKEY);
                   
                        XElement VKPMANDANT = new XElement("VKPMANDANT");
                         VKPMANDANT.Value = row.VKPMANDANT;
                          f.Add(VKPMANDANT);
                        XElement VKPAUFKRS = new XElement("VKPAUFKRS");
                         VKPAUFKRS.Value = row.VKPAUFKRS;
                          f.Add(VKPAUFKRS);
                        XElement VKPAUFLFD = new XElement("VKPAUFLFD");
                         VKPAUFLFD.Value = row.VKPAUFLFD;
                          f.Add(VKPAUFLFD);
                        XElement VKPAUFPOS = new XElement("VKPAUFPOS");
                         VKPAUFPOS.Value = row.VKPAUFPOS;
                          f.Add(VKPAUFPOS);
                        XElement VKPNEUDAT = new XElement("VKPNEUDAT");
                         VKPNEUDAT.Value = row.VKPNEUDAT;
                          f.Add(VKPNEUDAT);
                        XElement VKPKUNDNR = new XElement("VKPKUNDNR");
                         VKPKUNDNR.Value = row.VKPKUNDNR;
                          f.Add(VKPKUNDNR);
                        XElement VKPARTST = new XElement("VKPARTST");
                         VKPARTST.Value = row.VKPARTST;
                          f.Add(VKPARTST);
                        XElement VKPKOLL = new XElement("VKPKOLL");
                         VKPKOLL.Value = row.VKPKOLL;
                          f.Add(VKPKOLL);
                        XElement VKPART = new XElement("VKPART");
                         VKPART.Value = row.VKPART;
                          f.Add(VKPART);
                        XElement VKPFARBE = new XElement("VKPFARBE");
                         VKPFARBE.Value = row.VKPFARBE;
                          f.Add(VKPFARBE);
                        XElement VKPMENGE1 = new XElement("VKPMENGE1");
                         VKPMENGE1.Value = row.VKPMENGE1;
                          f.Add(VKPMENGE1);
                        XElement VKPMENGE2 = new XElement("VKPMENGE2");
                         VKPMENGE2.Value = row.VKPMENGE2;
                          f.Add(VKPMENGE2);
                        XElement VKPRESERV = new XElement("VKPRESERV");
                         VKPRESERV.Value = row.VKPRESERV;
                          f.Add(VKPRESERV);
                        XElement VKPZUGET = new XElement("VKPZUGET");
                         VKPZUGET.Value = row.VKPZUGET;
                          f.Add(VKPZUGET);
                        XElement VKPFREIGE = new XElement("VKPFREIGE");
                         VKPFREIGE.Value = row.VKPFREIGE;
                          f.Add(VKPFREIGE);
                        XElement VKPGAUFBEST = new XElement("VKPGAUFBEST");
                         VKPGAUFBEST.Value = row.VKPGAUFBEST;
                          f.Add(VKPGAUFBEST);
                        XElement VKPKOMMISS = new XElement("VKPKOMMISS");
                         VKPKOMMISS.Value = row.VKPKOMMISS;
                          f.Add(VKPKOMMISS);
                        XElement VKPFAKT = new XElement("VKPFAKT");
                         VKPFAKT.Value = row.VKPFAKT;
                          f.Add(VKPFAKT);
                        XElement VKPVERS = new XElement("VKPVERS");
                         VKPVERS.Value = row.VKPVERS;
                          f.Add(VKPVERS);
                        XElement VKPEINGET = new XElement("VKPEINGET");
                         VKPEINGET.Value = row.VKPEINGET;
                          f.Add(VKPEINGET);
                        XElement VKPOFFEN = new XElement("VKPOFFEN");
                         VKPOFFEN.Value = row.VKPOFFEN;
                          f.Add(VKPOFFEN);
                        XElement VKPMEEH = new XElement("VKPMEEH");
                         VKPMEEH.Value = row.VKPMEEH;
                          f.Add(VKPMEEH);
                        XElement VKPLIEFTERM = new XElement("VKPLIEFTERM");
                         VKPLIEFTERM.Value = row.VKPLIEFTERM;
                          f.Add(VKPLIEFTERM);
                  root.Add(f);
                }
                sw.Write(xdoc);
                sw.Close();
                sw.Dispose();
            }
            catch (Exception ex)
            { 
            }
      
        }                                        
    }
}
